package cn.learn.activiti.listener;

import cn.learn.activiti.domain.ActNodeConfig;
import cn.learn.activiti.util.ActThreadLocalContext;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.IdentityLinkEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityImpl;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 事件监听器
 *
 * @author huangyezhan
 * @version 1.0
 * @date 2021年03月18日13:03
 */
@Component
public class MyActivitiEventListener implements ActivitiEventListener {


    /**
     * 监听器代码，type种类很多（二三十种）,这里写了其中几种。
     *
     * @param activitiEvent 事件
     */
    @Override
    public void onEvent(ActivitiEvent activitiEvent) {
        switch (activitiEvent.getType()) {
            // 一个节点开始执行
            /*case ACTIVITY_STARTED:
                System.out.println("========================");
                break;
            // 一个节点执行结束
            case ACTIVITY_COMPLETED:
                System.out.println("============一个节点执行结束============");
                break;*/
            // 流程开始
            case PROCESS_STARTED:
                System.out.println("===========================流程开始===========================");
                break;
            // 流程结束
            case PROCESS_COMPLETED:
                System.out.println("===========================流程结束===========================");
                break;
            // 任务开始
            case TASK_CREATED:
                ActivitiEntityEventImpl activitiEntityEvent = (ActivitiEntityEventImpl) activitiEvent;
                TaskEntityImpl taskEntity = (TaskEntityImpl) activitiEntityEvent.getEntity();
                ExecutionEntity processInstance = taskEntity.getProcessInstance();
                System.out.println("任务节点ID：" + taskEntity.getTaskDefinitionKey() + "  任务ID：" + taskEntity.getId());
                /*
                 * TODO 根据业务，是否自动完成第一个节点
                 * TODO 或者自动完成该节点，并跳转到结束任务
                 * TODO 生成业务对应的数据信息，如节点动态表单等数据
                 */
                ActNodeConfig config = ActThreadLocalContext.getActNodeConfig();
                if (null != config) {
                    System.out.println(config + "============================================");
                }


                /*
                 * TODO 获取可处理任务的人员，对人员进行一些额外的操作
                 * TODO 发送待办消息，或者公告
                 */
                if (null != taskEntity.getAssignee()) {
                    System.out.println(taskEntity.getAssignee());
                } else {
                    List<IdentityLinkEntity> identityLinks = processInstance.getIdentityLinks();
                    for (IdentityLinkEntity link : identityLinks) {
                        // 是变量的，人员表里面才会有任务ID，要不然没有
                        if (taskEntity.getId().equals(link.getTaskId())) {
                            // starter participant
                            if ("participant".equals(link.getType())) {
                                System.out.println(link.getType());

                                if (null != link.getUserId()) {
                                    System.out.println(link.getUserId());
                                    if (link.getTaskId().equals("jack")){
                                        int  i =  1 / 0;
                                    }
                                }
                                if (null != link.getGroupId()) {
                                    System.out.println(link.getGroupId());
                                }
                            }
                        }
                    }
                }
                System.out.println("===========================任务开始===========================");
                break;
            // 任务完成
            case TASK_COMPLETED:
                System.out.println("===========================任务完成===========================");
                break;
            // 更新了已存在的实体。实体包含在事件中
            case ENTITY_UPDATED:
                System.out.println("=======================更新数据====================");
                ActivitiEntityEventImpl activitiEntityEvent2 = (ActivitiEntityEventImpl) activitiEvent;
                break;
            // 进程取消，删除实例
            case PROCESS_CANCELLED:
                System.out.println("===========================进程取消，删除实例===========================");
            default:
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

}
